package com.work.straighttalk.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMContactManager;
import com.hyphenate.exceptions.HyphenateException;

import com.work.straighttalk.R;
import com.work.straighttalk.bean.User;


import butterknife.Bind;

/**
 * Created by dllo on 16/3/4.
 */
public class SearchUserHolder extends BaseViewHolder<User> {

    private OnSearchListener listener;

    @Bind(R.id.avatar)
    ImageView avatar;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.btn_chat)
    Button btnChat;

    public SearchUserHolder(Context context, ViewGroup root, OnSearchListener listener) {
        super(context, root, R.layout.item_search_user);
        this.listener = listener;
    }

    @Override
    public void bindData(final User user) {
        name.setText(user.getUsername());
        btnChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        //添加好友
                        try {
                            EMClient.getInstance().contactManager().addContact(user.getUsername(), "加个好友怎么样");
                            listener.addFrends();
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }
}
